package com.spark.lola.configuration;

import com.spark.lola.feign.ConfFeignService;
import com.spark.lola.feign.GdsFeignService;
import com.spark.lola.feign.LogisticsFeignService;
import com.spark.lola.feign.SupFeignService;
import com.spark.lola.feign.service.PortalServiceOpenApi;
import com.spark.lola.logistics.mapper.*;
import com.spark.lola.logistics.service.impl.LogisticsFeignServiceImpl;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.service.gds.GdsFeignServiceImpl;
import com.spark.lola.service.gds.ICategoryInfoService;
import com.spark.lola.service.gds.IProductInfoService;
import com.spark.lola.service.pur.IOrderInfoService;
import com.spark.lola.service.sup.impl.SupFeignServiceImpl;

import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.configuration.CoreProperties;
import com.spark.lola.support.wms.configuration.JPushProperties;
import com.spark.lola.support.wms.configuration.WmsProperties;
import com.spark.lola.support.wms.job.WmsJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.List;

/**
 * @Author: DengYeJun
 * @Date: 2020/5/7 13:35
 */
@Configuration
@EnableKafka
@EnableConfigurationProperties(value = {CoreProperties.class, WmsProperties.class, JPushProperties.class})
public class RepairStatisticApiConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(RepairStatisticApiConfiguration.class);

    private ConfFeignService confFeignService;
    private WmsProperties wmsProperties;
    @Autowired
    public RepairStatisticApiConfiguration(ConfFeignService confFeignService){
        logger.info("===载入WMS配置===START===");
        ResponseBean responseBean = confFeignService.wms();
        if(responseBean.getCode()==200){
            wmsProperties = responseBean.convert(WmsProperties.class);
        }
        logger.info("===载入WMS配置===END===");

    }

    @Bean
    public WmsProperties wmsProperties(){
        return this.wmsProperties;
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder(10);
    }


    /**
     * 统一异常处理
     *
     * @param errorAttributes    异常属性
     * @param serverProperties   服务属性
     * @param errorViewResolvers 异常处理视图
     * @return 控制类
     */
    @Bean
    public BasicErrorController basicErrorController(ErrorAttributes errorAttributes,
                                                     ServerProperties serverProperties,
                                                     List<ErrorViewResolver> errorViewResolvers) {
        return new BasicErrorController(errorAttributes, serverProperties.getError(),
                errorViewResolvers);
    }

    @Bean
    public LogisticsFeignService logisticsFeignService(LocationInfoMapper locationInfoMapper, CarSubscribeMapper carSubscribeMapper,
                                                       LogisticsOrderInfoMapper logisticsOrderInfoMapper, ExpensesInfoMapper expensesInfoMapper,
                                                       DriverInfoMapper driverInfoMapper, CarInfoMapper carInfoMapper) {
        return new LogisticsFeignServiceImpl(locationInfoMapper, carSubscribeMapper, logisticsOrderInfoMapper, expensesInfoMapper,
                driverInfoMapper, carInfoMapper);
    }

    @Bean
    public GdsFeignService gdsFeignService(ICategoryInfoService categoryInfoService, IProductInfoService productInfoService,
                                           LogisticsFeignService logisticsFeignService) {
        return new GdsFeignServiceImpl(categoryInfoService, productInfoService, logisticsFeignService);
    }

    @Bean
    public SupFeignService supFeignService(SupplierInfoMapper supplierInfoMapper, IOrderInfoService orderInfoService, WmsProperties wmsProperties) {
        return new SupFeignServiceImpl(supplierInfoMapper, orderInfoService, wmsProperties);
    }

    @Bean
    public WmsJob wmsJob(PortalServiceOpenApi portalServiceOpenApi) {
        return new WmsJob(portalServiceOpenApi);
    }


}
